﻿//======================================================================
//
//	Cross-Platform XNA Menu System
//  http://cpxms.codeplex.com/
//
//  All code and content licensed under Microsoft Public License (Ms-PL)	
//
//======================================================================
//
//	File:		ColorPicker.cs
//
//	Author:		MJP
//
//	Date:		04/17/09
//
//======================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.IO;
using System.Threading;
using System.Diagnostics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

using XnaColor = Microsoft.Xna.Framework.Graphics.Color;

namespace CPXMenu
{
	/// <summary>
	/// Allows the user to pick a color by manipulating the individual channels via sliders
	/// </summary>
    internal partial class ColorPicker : UserControl
    {
        XnaColor color;
        System.Drawing.Rectangle colorRect = new System.Drawing.Rectangle(235, 60, 40, 40);

		/// <summary>
		/// The Color selected by the user
		/// </summary>
        public XnaColor Color
        {
            get { return color; }
        }

		/// <summary>
		/// Creates a ColorPicker initialized to the specified color
		/// </summary>
		/// <param name="color"></param>
        public ColorPicker(XnaColor color)
        {
            InitializeComponent();

            this.color = color;
            redSlider.Value = color.R;
            greenSlider.Value = color.G;
            blueSlider.Value = color.B;
            alphaSlider.Value = color.A;
        }

		/// <summary>
		/// Red slider changed
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void redSlider_ValueChanged(object sender, EventArgs e)
        {
            color.R = (byte)redSlider.Value;
            redLabel.Text = redSlider.Value.ToString();
            this.Invalidate(colorRect);
        }

		/// <summary>
		/// Green slider changed
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void greenSlider_ValueChanged(object sender, EventArgs e)
        {
            color.G =(byte)greenSlider.Value;
            greenLabel.Text = greenSlider.Value.ToString();
            this.Invalidate(colorRect);
        }

		/// <summary>
		/// Blue slider changed
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void blueSlider_ValueChanged(object sender, EventArgs e)
        {
            color.B = (byte)blueSlider.Value;
            blueLabel.Text = blueSlider.Value.ToString();
            this.Invalidate(colorRect);
        }

        /// <summary>
        /// Alpha slider changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void alphaSlider_ValueChanged(object sender, EventArgs e)
        {
            color.A = (byte)alphaSlider.Value;
            alphaLabel.Text = alphaSlider.Value.ToString();
            this.Invalidate(colorRect);
        }

		/// <summary>
		/// WM_PAINT handler.  Draws a rectangle with the selected color.
		/// </summary>
		/// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            
            System.Drawing.Color drawColor = System.Drawing.Color.FromArgb(alphaSlider.Value, redSlider.Value, greenSlider.Value, blueSlider.Value);
            SolidBrush brush = new SolidBrush(drawColor);
            e.Graphics.FillRectangle(brush, colorRect);            
        }
        
    }

    /// <summary>
    /// Used to allow the user to set a color in the PropertyGrid
    /// </summary>
    public class ColorPickerEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(ITypeDescriptorContext context,
                                            IServiceProvider provider,
                                            object value)
        {
            IWindowsFormsEditorService editorService = null;

            if (provider != null)
            {
                editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }

            if (editorService != null)
            {
                // Pop up our dialog
                ColorPicker colorPicker = new ColorPicker((XnaColor)value);

                editorService.DropDownControl(colorPicker);

                value = colorPicker.Color;
            }

            return value;
        }

    }
}
